package com.example.mydy.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

/**
 * SharedPreferences工具类
 */
public class SharedPreferenceUtils {
    private static final String TAG = "SharedPreferenceUtils";
    private static final String PREFS_NAME = "MyAppPrefs";
    private static final String KEY_TOKEN = "token";
    
    /**
     * 保存字符串
     */
    public static void saveString(Context context, String key, String value) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(key, value);
        
        // 特殊处理token
        if (KEY_TOKEN.equals(key)) {
            // 保存到NetworkUtils缓存
            Log.d(TAG, "正在保存token到NetworkUtils");
            NetworkUtils.saveAuthToken(context, value);
        }
        
        editor.commit();
    }
    
    /**
     * 获取字符串
     */
    public static String getString(Context context, String key, String defValue) {
        // 特殊处理token
        if (KEY_TOKEN.equals(key)) {
            // 优先从NetworkUtils获取
            String token = NetworkUtils.getAuthToken(context);
            if (token != null && !token.isEmpty()) {
                return token;
            }
        }
        
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        return prefs.getString(key, defValue);
    }
    
    /**
     * 保存布尔值
     */
    public static void saveBoolean(Context context, String key, boolean value) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        prefs.edit().putBoolean(key, value).commit();
    }
    
    /**
     * 获取布尔值
     */
    public static boolean getBoolean(Context context, String key, boolean defValue) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        return prefs.getBoolean(key, defValue);
    }
    
    /**
     * 保存整数
     */
    public static void saveInt(Context context, String key, int value) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        prefs.edit().putInt(key, value).commit();
    }
    
    /**
     * 获取整数
     */
    public static int getInt(Context context, String key, int defValue) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        return prefs.getInt(key, defValue);
    }
    
    /**
     * 保存长整数
     */
    public static void saveLong(Context context, String key, long value) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        prefs.edit().putLong(key, value).commit();
    }
    
    /**
     * 获取长整数
     */
    public static long getLong(Context context, String key, long defValue) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        return prefs.getLong(key, defValue);
    }
    
    /**
     * 保存浮点数
     */
    public static void saveFloat(Context context, String key, float value) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        prefs.edit().putFloat(key, value).commit();
    }
    
    /**
     * 获取浮点数
     */
    public static float getFloat(Context context, String key, float defValue) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        return prefs.getFloat(key, defValue);
    }
    
    /**
     * 移除指定键值
     */
    public static void remove(Context context, String key) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.remove(key);
        
        // 特殊处理token
        if (KEY_TOKEN.equals(key)) {
            // 同时清除NetworkUtils中的token
            Log.d(TAG, "正在从NetworkUtils清除token");
            NetworkUtils.clearAuthToken(context);
        }
        
        editor.commit();
    }
    
    /**
     * 清空所有数据
     */
    public static void clear(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.clear();
        
        // 同时清除NetworkUtils中的token
        Log.d(TAG, "正在从NetworkUtils清除token");
        NetworkUtils.clearAuthToken(context);
        
        editor.commit();
    }
} 